namespace CIS
{
    using CISLib;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceInfoCountPage : Page
    {
        protected Image iCustomerInfo;
        protected Label lbResourceType;
        protected UserPrincipal Principal = null;
        protected DataGrid resourceListDg;
        protected DropDownList ResourceTypeDdl;
        protected Button SearchBtn;

        private void AddRow(DataTable myTable)
        {
            int count = 0;
            for (int i = 0; i < myTable.Rows.Count; i++)
            {
                count += (int) myTable.Rows[i][1];
            }
            object[] rowVals = new object[2];
            DataRowCollection rc = myTable.Rows;
            rowVals[0] = "All";
            rowVals[1] = count;
            DataRow myNewRow = rc.Add(rowVals);
        }

        private void InitializeComponent()
        {
            this.SearchBtn.Click += new EventHandler(this.SearchBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.ResourceTypeDdl.Items.Clear();
            ListItem tmpItem1 = new ListItem();
            tmpItem1.Text = "协议酒店信息";
            tmpItem1.Value = "1";
            this.ResourceTypeDdl.Items.Add(tmpItem1);
            ListItem tmpItem2 = new ListItem();
            tmpItem2.Text = "推存餐饮信息";
            tmpItem2.Value = "2";
            this.ResourceTypeDdl.Items.Add(tmpItem2);
            ListItem tmpItem3 = new ListItem();
            tmpItem3.Text = "休闲娱乐";
            tmpItem3.Value = "3";
            this.ResourceTypeDdl.Items.Add(tmpItem3);
            ListItem tmpItem4 = new ListItem();
            tmpItem4.Text = "服务代理";
            tmpItem4.Value = "4";
            this.ResourceTypeDdl.Items.Add(tmpItem4);
            ListItem tmpItem5 = new ListItem();
            tmpItem5.Text = "医院";
            tmpItem5.Value = "5";
            this.ResourceTypeDdl.Items.Add(tmpItem5);
            ListItem tmpItem6 = new ListItem();
            tmpItem6.Text = "展览活动";
            tmpItem6.Value = "6";
            this.ResourceTypeDdl.Items.Add(tmpItem6);
            ListItem tmpItem7 = new ListItem();
            tmpItem7.Text = "特色节日";
            tmpItem7.Value = "7";
            this.ResourceTypeDdl.Items.Add(tmpItem7);
            ListItem tmpItem8 = new ListItem();
            tmpItem8.Text = "常用电话";
            tmpItem8.Value = "8";
            this.ResourceTypeDdl.Items.Add(tmpItem8);
            ListItem tmpItem9 = new ListItem();
            tmpItem9.Text = "分公司通信录";
            tmpItem9.Value = "9";
            this.ResourceTypeDdl.Items.Add(tmpItem9);
            ListItem tmpItem10 = new ListItem();
            tmpItem10.Text = "其他信息";
            tmpItem10.Value = "10";
            this.ResourceTypeDdl.Items.Add(tmpItem10);
            ListItem tmpItem11 = new ListItem();
            tmpItem11.Text = "销售小技巧";
            tmpItem11.Value = "11";
            this.ResourceTypeDdl.Items.Add(tmpItem11);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFrom();
                this.SetFromImage();
            }
        }

        protected string RegionName()
        {
            if ((this.ResourceTypeDdl.SelectedValue == "10") || (this.ResourceTypeDdl.SelectedValue == "11"))
            {
                return "NoRegionName";
            }
            return "RegionName";
        }

        private void resourceListDgDataBind(string tableName, string columnName)
        {
            DataTable dt = ManageResourceCount.GetCount(tableName, columnName).Tables[0];
            this.AddRow(dt);
            this.resourceListDg.DataSource = dt;
            this.resourceListDg.DataBind();
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            switch (this.ResourceTypeDdl.SelectedValue)
            {
                case "1":
                    this.resourceListDgDataBind("TB_ResourceHotel", "HotelProvince");
                    break;

                case "2":
                    this.resourceListDgDataBind("TB_ResourceRestaurant", "RestaurantProvince");
                    break;

                case "3":
                    this.resourceListDgDataBind("TB_ResourceEntertainment", "EntertainmentProvince");
                    break;

                case "4":
                    this.resourceListDgDataBind("TB_ResourceAgencies", "AgenciesProvince");
                    break;

                case "5":
                    this.resourceListDgDataBind("TB_ResourceHospital", "HospitalProvince");
                    break;

                case "6":
                    this.resourceListDgDataBind("TB_ResourceEvents", "EventsProvince");
                    break;

                case "7":
                    this.resourceListDgDataBind("TB_ResourceHoliday", "HolidayProvince");
                    break;

                case "8":
                    this.resourceListDgDataBind("TB_ResourceTelephone", "TelephoneProvince");
                    break;

                case "9":
                    this.resourceListDgDataBind("TB_ResourceDirectory", "DirectoryProvince");
                    break;

                case "10":
                {
                    DataTable dt = ManageResourceCount.GetCount("TB_ResourceOther").Tables[0];
                    this.resourceListDg.DataSource = dt;
                    this.resourceListDg.DataBind();
                    break;
                }
                case "11":
                {
                    DataTable dt = ManageResourceCount.GetCount("TB_ResourceTip").Tables[0];
                    this.resourceListDg.DataSource = dt;
                    this.resourceListDg.DataBind();
                    break;
                }
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbResourceType.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceInfoCountPage", "lbResourceType", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.resourceListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceInfoCountPage", "lbProvinceOverSea", language, path);
            }
            else
            {
                this.resourceListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceInfoCountPage", "lbProvince", language, path);
            }
            this.resourceListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("ResourceInfoCountPage", "count", language, path);
            this.SearchBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceInfoCountPage", "SearchBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCustomerInfo.ImageUrl = "images/title_ResourceCount.jpg";
                    break;

                case "en":
                    this.iCustomerInfo.ImageUrl = "images/title_ResourceCountEn.jpg";
                    break;
            }
        }
    }
}

